package com.elite.groovy.dev;

import com.alibaba.cloud.commons.io.IOUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @projectName: ngsapitask-cdcs
 * @package: com.elite.groovy.kh
 * @className: Test
 * @author: LiJT
 * @description: TODO
 * @date: 2023-05-05 20:17
 * @version: 1.0
 */
public class TestTao {
    private static final Log log = LogFactory.getLog(TestTao.class);

    public static void main(String[] args) throws Exception {

        String cdr_cno = readFileToString("C:\\Users\\LiJT\\Desktop\\clink.txt");

        JSONObject doGETJSON = JSONObject.fromObject(cdr_cno);
        log.debug("1-呼入通话记录 Data Result:" + doGETJSON);

        JSONArray jsonArray = doGETJSON.getJSONArray("cdrIbs");
        ;
        int dataArrayLen = jsonArray.size();
        log.debug("1-呼入通话记录 Data size:" + dataArrayLen);

        List<JSONObject> list = new ArrayList<JSONObject>();
        for (int i = 0; i < jsonArray.size(); i++) {
            list.add(jsonArray.getJSONObject(i));
        }

        Collections.sort(list, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject o1, JSONObject o2) {
                String name1 = o1.getString("startTime");
                String name2 = o2.getString("startTime");
                return name1.compareTo(name2);
            }
        });

        System.out.println(list.size());
        for (int i = 0; i < list.size(); i++) {
            //System.out.println(list.get(i).get("customerNumber"));
            if (list.get(i).get("customerNumber").equals("15600017888") ) {
                System.out.println(list.get(i).get("customerNumber") + "    " + list.get(i).get("startTime"));
            }
        }


    }


    public static int countOccurrences(String str, String subStr) {
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(subStr, index)) != -1) {
            count++;
            index += subStr.length();
        }
        return count;
    }

    public static String readFileToString(String filePath) {
        String jsonString = null;
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(filePath);
            jsonString = IOUtils.toString(fileInputStream, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return jsonString;
    }
}
